#include "DeviceChannelTableWidget.h"
#include <QHeaderView>
#include <QPushButton>
#include <QMessageBox>
#include <QInputDialog>
#include <QColorDialog>

DeviceChannelTableWidget::DeviceChannelTableWidget(QWidget *parent)
    : QTableWidget(parent)
{
    initTable();
}

void DeviceChannelTableWidget::initTable()
{
    // 设置表头
    QStringList headers;
    headers << "启用" << "通道名称" << "放大器名称" << "测量范围" << "单位" << "模式" << "操作";
    setColumnCount(headers.size());
    setHorizontalHeaderLabels(headers);

    // 设置表格属性
    setSelectionBehavior(QAbstractItemView::SelectRows);
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    verticalHeader()->setVisible(false);
}

void DeviceChannelTableWidget::addDeviceChannel(DeviceChannel *channel)
{
    if (!channel) return;

    // 添加到数据源
    deviceChannels.append(channel);

    // 刷新表格
    refreshTable();
}

void DeviceChannelTableWidget::addDeviceSubChannel(DeviceChannel *parentChannel, DeviceSubChannel *subChannel)
{
    if (!parentChannel || !subChannel) return;

    // 添加到父通道的子通道列表
    parentChannel->addSubChannel(subChannel);

    // 刷新表格
    refreshTable();
}

void DeviceChannelTableWidget::refreshTable()
{
    // 清空表格
    setRowCount(0);

    int row = 0;

    // 遍历所有 DeviceChannel
    for (DeviceChannel *channel : deviceChannels) {
        // 添加 DeviceChannel 行
        insertRow(row);

        // 第一列：启用按钮
        QPushButton *enableButton = new QPushButton("启用", this);
        enableButton->setCheckable(true); // 设置为可选中状态
        enableButton->setChecked(channel->m_bEnabled); // 根据 m_bEnabled 设置按钮状态
        connect(enableButton, &QPushButton::clicked, this, [this, channel](bool checked) {
            channel->m_bEnabled = checked; // 更新启用状态
        });
        setCellWidget(row, 0, enableButton);

        // 其他列：显示通道信息
        setItem(row, 1, new QTableWidgetItem(channel->m_strChannelName));
        setItem(row, 2, new QTableWidgetItem(channel->m_strAmplifierName));
        setItem(row, 3, new QTableWidgetItem(QString("%1 - %2").arg(channel->m_dlbMeasureRangeMin).arg(channel->m_dlbMeasureRangeMax)));
        setItem(row, 4, new QTableWidgetItem(channel->m_strUnit));
        setItem(row, 5, new QTableWidgetItem(QString::number(channel->m_nChannelMode)));

        // 最后一列：设置按钮
        QPushButton *settingsButton = new QPushButton("设置", this);
        connect(settingsButton, &QPushButton::clicked, this, &DeviceChannelTableWidget::onSettingsButtonClicked);
        setCellWidget(row, 6, settingsButton);

        // 设置背景颜色
        item(row, 1)->setBackground(channel->m_cColor);

        row++;

        // 添加 DeviceSubChannel 行
        for (DeviceSubChannel *subChannel : channel->subChannels()) {
            insertRow(row);

            // 第一列：留空
            setItem(row, 0, new QTableWidgetItem("")); // 第一列留空

            // 其他列：显示子通道信息
            setItem(row, 1, new QTableWidgetItem("    " + subChannel->m_strChannelName)); // 缩进
            setItem(row, 2, new QTableWidgetItem(subChannel->m_strAmplifierName));
            setItem(row, 3, new QTableWidgetItem(QString("%1 - %2").arg(subChannel->m_dlbMeasureRangeMin).arg(subChannel->m_dlbMeasureRangeMax)));
            setItem(row, 4, new QTableWidgetItem(subChannel->m_strUnit));
            setItem(row, 5, new QTableWidgetItem(QString::number(subChannel->m_nChannelMode)));

            // 最后一列：... 按钮
            QPushButton *moreButton = new QPushButton("...", this);
            connect(moreButton, &QPushButton::clicked, this, [this, channel]() {
                onMoreButtonClicked(channel); // 传递父通道指针
            });
            setCellWidget(row, 6, moreButton);

            // 设置背景颜色
            item(row, 1)->setBackground(subChannel->m_cColor);

            row++;
        }
    }
}

void DeviceChannelTableWidget::onSettingsButtonClicked()
{
    // 弹出对话框，输入父通道数据
    bool ok;
    QString channelName = QInputDialog::getText(this, "添加父通道", "通道名称:", QLineEdit::Normal, "", &ok);
    if (ok && !channelName.isEmpty()) {
        // 创建新的父通道
        DeviceChannel *channel = new DeviceChannel(this);
        channel->m_strChannelName = channelName;
        channel->m_strAmplifierName = "Amplifier A"; // 默认值
        channel->m_dlbMeasureRangeMin = 0.0; // 默认值
        channel->m_dlbMeasureRangeMax = 10.0; // 默认值
        channel->m_strUnit = "V"; // 默认值
        channel->m_nChannelMode = 1; // 默认值
        channel->m_cColor = Qt::red; // 默认颜色
        // 添加到表格
        addDeviceChannel(channel);
    }
}

void DeviceChannelTableWidget::onMoreButtonClicked(DeviceChannel *parentChannel)
{
    if (!parentChannel) return;

    // 弹出对话框，输入子通道数据
    bool ok;
    QString subChannelName = QInputDialog::getText(this, "添加子通道", "子通道名称:", QLineEdit::Normal, "", &ok);
    if (ok && !subChannelName.isEmpty()) {
        // 创建新的子通道
        DeviceSubChannel *subChannel = new DeviceSubChannel(this);
        subChannel->m_strChannelName = subChannelName;
        subChannel->m_strAmplifierName = "Amplifier B"; // 默认值
        subChannel->m_dlbMeasureRangeMin = 0.0; // 默认值
        subChannel->m_dlbMeasureRangeMax = 5.0; // 默认值
        subChannel->m_strUnit = "mV"; // 默认值
        subChannel->m_nChannelMode = 2; // 默认值
        subChannel->m_cColor = Qt::blue; // 默认颜色

        // 添加到父通道
        addDeviceSubChannel(parentChannel, subChannel);
    }
}
